package com.sailfish.springbootdemo.controller.db1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceType;
import com.sailfish.springbootdemo.pojo.db1.TechCapacityRule;
import com.sailfish.springbootdemo.pojo.db1.TechDeviceParam;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.HttpUtil;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/thirdApi")
@CrossOrigin
public class ThirdApiController {

    @Value("${personal.config.apis.sign}")
    private String sign;

    @Value("${personal.config.apis.get-all-capacity-request-key-api}")
    private String getAllCapacityRequestKeyApi;

    @Value("${personal.config.apis.get-board-request-key-api}")
    private String getBoardRequestKeyApi;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @PostMapping("/getAllCapacityRequestKey")
    public Result getAllCapacityRequestKey(String type) {
        try {
            JSONObject params = new JSONObject();
            String md5Sign = CommonUtils.encryptToMD5(CommonUtils.encryptToMD5(sign));
            params.put("sign", md5Sign);

            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getBoardRequestKeyApi 入参：" + params);
            String keyRes1 = HttpUtil.sendPostJson(getBoardRequestKeyApi, params);
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getBoardRequestKeyApi 返回：" + keyRes1);
            JSONObject resObj1 = JSONObject.parseObject(keyRes1);
            JSONObject boardInfoJo = new JSONObject();
            if ("200".equals(resObj1.getString("Code"))) {
                boardInfoJo = resObj1.getJSONObject("Data");
            } else {
                return ResultUtil.error(500, resObj1.getString("Msg"), resObj1.getString("Msg"), null);
            }

            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getAllCapacityRequestKeyApi 入参：" + params);
            String keyRes = HttpUtil.sendPostJson(getAllCapacityRequestKeyApi, params);
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getAllCapacityRequestKeyApi 返回：" + keyRes);
            JSONObject resObj = JSONObject.parseObject(keyRes);
            if ("200".equals(resObj.getString("Code"))) {
                JSONArray data2 = resObj.getJSONArray("Data2");
                JSONArray finalJa = new JSONArray();
                for (int a = 0; a < data2.size(); a++) {
                    JSONObject jsonObject = data2.getJSONObject(a);
                    String key = jsonObject.getString("key");
                    String notes = jsonObject.getString("notes");
                    String belongTo = jsonObject.getString("belongTo");
                    String value = jsonObject.getString("value");
                    String t = jsonObject.getString("type");

                    if (belongTo != null && !"".equals(belongTo)) {
                        for (String k : boardInfoJo.keySet()) {
                            if (k.equals(belongTo)) {
                                JSONObject boardJ = new JSONObject();
                                boardJ.put("key", k);
                                boardJ.put("value", boardInfoJo.getString(k));
                                jsonObject.put("boardInfo", boardJ);
                                break;
                            }
                        }
                    } else {
                        jsonObject.put("boardInfo", null);
                    }


                    if (type.equals(t)) {
                        JSONObject finalJo = new JSONObject();
                        finalJo = data2.getJSONObject(a);

                        JSONArray valueJa = new JSONArray();
                        // 解析 value 循环匹配 设备基线参数池、设备工艺参数池、整线工艺
                        if (value != null && !"".equals(value)) {
                            if (value.contains("\n")) {
                                value = value.replaceAll("\\n", "");
                            }
                            if (value.contains("\r")) {
                                value = value.replaceAll("\\r", "");
                            }
                            if (value.contains("^^")) {
                                String[] split = value.split("\\^\\^");
                                for (String s : split) {
                                    valueJa.add(s);
                                }
                            } else {
                                valueJa.add(value);
                            }
                        }

                        JSONArray ja = new JSONArray();
                        for (int b = 0; b < valueJa.size(); b++) {
                            String keyString = valueJa.getString(b);
                            // 循环匹配 设备基线参数池、设备工艺参数池、整线工艺 直到找到一个为止
                            // keyType  1:基线参数池   2:工艺参数池   3:工艺整线  4:其他特殊无对应关系的
                            JSONObject jo = new JSONObject();
                            PcbaDeviceParam pcbaDeviceParamByKey = pcbaDeviceParamDao.getPcbaDeviceParamByKey(keyString);
                            if (pcbaDeviceParamByKey != null) {
                                Integer pcbaDeviceTypeId = pcbaDeviceParamByKey.getPcbaDeviceTypeId();
                                pcbaDeviceParamByKey.setPcbaDevice(pcbaDeviceTypeDao.getById(pcbaDeviceTypeId));
                                jo.put("keyString", keyString);
                                jo.put("keyType", 1);
                                jo.put("details", pcbaDeviceParamByKey);
                            } else {
                                TechDeviceParam techDeviceParamByKey = techDeviceParamDao.getTechDeviceParamByKey(keyString);
                                if (techDeviceParamByKey != null) {
                                    Integer pcbaDeviceTypeId = techDeviceParamByKey.getPcbaDeviceTypeId();
                                    techDeviceParamByKey.setPcbaDeviceType(pcbaDeviceTypeDao.getById(pcbaDeviceTypeId));
                                    jo.put("keyString", keyString);
                                    jo.put("keyType", 2);
                                    jo.put("details", techDeviceParamByKey);
                                } else {
                                    TechCapacityRule techCapacityRuleByKey = techCapacityRuleDao.getTechCapacityRuleByKey(keyString);
                                    if (techCapacityRuleByKey != null) {
                                        Integer lineTechBigTypeId = techCapacityRuleByKey.getLineTechBigTypeId();
                                        techCapacityRuleByKey.setLineTechBigType(lineTechBigTypeDao.getLineTechBigTypeById(lineTechBigTypeId));
                                        jo.put("keyString", keyString);
                                        jo.put("keyType", 3);
                                        jo.put("details", techCapacityRuleByKey);
                                    } else {
                                        jo.put("keyString", keyString);
                                        jo.put("keyType", 4);
                                        jo.put("details", null);
                                    }
                                }
                            }
                            ja.add(jo);
                        }

                        finalJo.put("valueJa", ja);
                        finalJa.add(finalJo);
                    }
                }
                return ResultUtil.success(finalJa);
            }
            return ResultUtil.error(500, resObj.getString("Msg"), resObj.getString("Msg"), null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
